package com.cbts.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.cbts.dao.DepartmentDao;
import com.cbts.entity.Department;
import com.cbts.util.Page;
/**
 * @author 邱鸿辉
 *
 */
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao{
	
	public Department getDepartmentByDepartmentName(String departmentName) {
		
		String hql = "and o.departmentName =?";
		return queryUnique(hql, new Object[]{departmentName});
	}

	@SuppressWarnings("unchecked")
	public void updateDepartmentList(final List<Department> departmentList) {
		
		getHibernateTemplate().execute(new HibernateCallback(){
			 public Object doInHibernate(Session session) throws HibernateException, SQLException {
				 List<String> departmentNames = new ArrayList<String>();
				for (int i = 0; i < departmentList.size(); i++)
				{
					departmentNames.add(departmentList.get(i).getDepartmentName());
					String s = UUID.randomUUID().toString();
					departmentList.get(i).setDepartmentName(s);
					System.out.println("department.getDepartmentName():"+departmentList.get(i).getDepartmentName());
					getHibernateTemplate().update(departmentList.get(i));
					if(i%50==0){	//清空session
						session.flush();
						session.clear();
					}
				}
				session.flush();
				session.clear();

				for (int i = 0; i < departmentList.size(); i++) {
					departmentList.get(i).setDepartmentName(departmentNames.get(i));
					getHibernateTemplate().update(departmentList.get(i));
					if(i%50==0){	//清空session
						session.flush();
						session.clear();
					}
				}	
				session.flush();
				session.clear();
				return null;
			 	}
			}
		);
	}
}
